package com.woniu.backparking.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.backparking.entity.Cart;
import com.woniu.backparking.form.SelectForm;
import com.woniu.backparking.service.CartService;
import com.woniu.backparking.util.BaseEnum;
import com.woniu.backparking.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;

/**
 * <p>
 * 车位表 前端控制器
 * </p>
 *
 * @author HashSong
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/cart")
@CrossOrigin
@Slf4j
public class CartController {
    @Resource
    private CartService cartService;
//    @Autowired
//    private RedissonClient redissonClient; //  /usr/share/nginx/html
    /*
    分页条件查询车位
    参数 分页数据 小区id 车位收费类型id
    return 分页数据 车位集合数据
     */
    @GetMapping("showCart")
    public JSONResult findByCondition(SelectForm selectForm,int available){
        log.info("进入展示:{}",selectForm);
        Page<Cart> cartPage = new Page<>();
        cartPage.setSize(selectForm.getSize());
        cartPage.setCurrent(selectForm.getCurrent());
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(selectForm.getCommunityId()!=0,"community_id",selectForm.getCommunityId());
        //判断是否传入typeId
        queryWrapper.eq(selectForm.getTypeId()!=0,"type_id",selectForm.getTypeId());
        queryWrapper.eq(available!=2,"available",available);
//        queryWrapper.eq("available",1);
        Page<Cart> page = cartService.page(cartPage, queryWrapper);
//        RList<Cart> carts = redissonClient.getList("carts");
//        for(Iterator<Cart> iterator = page.getRecords().iterator(); iterator.hasNext();){
//            carts.add(iterator.next());
//        }

        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,cartPage,page.getRecords());
    }
    @PutMapping("updateState")
    public JSONResult updateState(Cart cart){
        log.info("更改车位状态:id{}-state{}",cart.getCartId(),cart.getAvailable());
        cartService.updateById(cart);
        return new JSONResult(BaseEnum.StatusCode.Success, BaseEnum.StatusMessage.Success,null,null);
    }
    @GetMapping("showCart02")
    @Cacheable(cacheNames = "showCart02",key="#current")
    public void findByCondition(int current){
        log.info("进入展示我无敌:{}",current);

    }
}

